package controller;
import java.awt.Color;import javax.swing.JOptionPane;import model.Prescription;import model.PrescriptionBox;import model.PrescriptionLabel;import view.Canvas;import view.Designer;import view.toolboxComponents.*;/** * An event handler class for the toolbox panel on the designer * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer * */
public class ToolBoxEventHandler extends EventHandler{	/**	 * Creates the appropriate event from a message passed to this method	 * @param the event object with the message needed to identify the behaviour required.	 */	public void createEvent(CustomEvent event) 	{		Prescription prescription = Prescription.getInstance();		Designer designer = Designer.getInstance();		Canvas canvas = Canvas.getInstance();
		if (event.getName().equals("bgColorChange"))
		{	
			prescription.changeBGColour();
			Color c = prescription.getBGColour();
			((PrescriptionBackgroundPanel) event.getSource()).setButtonBackgroundColour(c);			canvas.repaint();			return;
		}

		if (event.getName().equals("presHeightChange"))
		{
			int height = 0;

			try{
				height = Integer.parseInt((String) event.getAttached());
			}
			catch (NumberFormatException e){
				System.err.println("Integer height value required");
			}

			if(height > 10 && height < 2000){
				try{
					prescription.changePrescriptionHeight((String)event.getAttached());
				}
				catch (NumberFormatException e){					designer.showMessageDialog("Height is invalid", "Invalid Input", JOptionPane.WARNING_MESSAGE);
				}
				designer.update();				canvas.repaint();				return;
			}
			else			{				designer.showMessageDialog("Height is invalid", "Invalid Input", JOptionPane.WARNING_MESSAGE);				canvas.repaint();				return;
			}
		}
		if (event.getName().equals("presWidthChange"))
		{
			int width = 0;
			try{ 
				width = Integer.parseInt((String) event.getAttached());				
			}
			catch (NumberFormatException e){
				System.err.println("Integer width value required");
			}

			if(width > 10 && width < 2000){
				try{
					prescription.changePrescriptionWidth((String)event.getAttached());
				}
				catch (NumberFormatException e){
					designer.showMessageDialog("Width is invalid", "Invalid Input", JOptionPane.WARNING_MESSAGE);
				}

				designer.update();				canvas.repaint();				return;
			}
			else{
				designer.showMessageDialog("Width is invalid", "Invalid Input", JOptionPane.WARNING_MESSAGE);				canvas.repaint();				return;
			}
		}

		if (event.getName().equals("presLineColourChange"))
		{
			if (prescription.objects().size() != 0)
			{
				prescription.changeLineColour();
				Color c = prescription.objects().get(prescription.getSelectedIndex()).getLineColour();
				((PrescriptionLineColourPanel) event.getSource()).setButtonBackgroundColour(c);				canvas.repaint();				return;
			}else{				designer.showMessageDialog("Add an object first", "Invalid Input", JOptionPane.WARNING_MESSAGE);				canvas.repaint();				return;			}
		}

		if(event.getName().equals("presLineWidthChange"))
		{
			float lineWidth = (Float) event.getAttached();
			prescription.changeLineWidth(lineWidth);			canvas.repaint();			return;
		}

		if(event.getName().equals("presNameChange"))
		{
			String name = (String) event.getAttached();
			prescription.setName(name);
			designer.update();			canvas.repaint();			return;
		}

		//Change Object Fill Colour
		if (event.getName().equals("presObjectFillColourChange"))
		{	
			if (prescription.objects().size() != 0){
				prescription.changeFillColourAll();
				Color c = prescription.objects().get(prescription.getSelectedIndex()).getFillColour();
				((PrescriptionObjectFillPanel) event.getSource()).setButtonBackgroundColour(c);				canvas.repaint();				return;
			}else{				designer.showMessageDialog("Add an object first", "Invalid Input", JOptionPane.WARNING_MESSAGE);				canvas.repaint();				return;			}
		}

		if (event.getName().equals("presObjectNameChange")){
			if (prescription.objects().size() == 0){				designer.showMessageDialog("Add an object first", "Invalid Input", JOptionPane.WARNING_MESSAGE);				canvas.repaint();				return;
			}else{
				prescription.changeName((String)event.getAttached());				canvas.repaint();				return;
			}
		}

		if (event.getName().equals("presSerialNumberChange")){
			prescription.setSerialNumber((String)event.getAttached());
			designer.update();			canvas.repaint();			return;
		}

		if(event.getName().equals("presTextColourChange"))
		{
			if (prescription.objects().size() != 0)
			{
				prescription.changeTextColour();
				Color c = prescription.objects().get(prescription.getSelectedIndex()).getTextColour();
				((PrescriptionTextColourPanel) event.getSource()).setButtonBackgroundColour(c);				canvas.repaint();				return;
			}else{				designer.showMessageDialog("Add an object first", "Invalid Input", JOptionPane.WARNING_MESSAGE);				canvas.repaint();				return;			}
		}
		if(event.getName().equals("presAddObject"))
		{
			Object[] options = {"Box", "Label"};			int selectedOption = JOptionPane.showOptionDialog(Designer.getInstance(), "Please select an object", "Select an object", 					JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null,					options, options[0]);						if(selectedOption == 0){ //if the selection was a box				prescription.addObject(new PrescriptionBox());				prescription.setSelected(prescription.objects().size()-1);				designer.update();				canvas.repaint();				return;			}else if(selectedOption == 1){ //if the selection was a label				prescription.addObject(new PrescriptionLabel());				prescription.setSelected(prescription.objects().size()-1);				designer.update();				canvas.repaint();				return;			}else if(selectedOption == -1){ //if they closed the dialog				return;			}			Designer.getInstance().showMessageDialog("cannot add object"); //error sweeping code.			return;
		}
		if(event.getName().equals("presSetTransparent"))
		{
			prescription.transparent();			canvas.repaint();			return;
		}
		if(event.getName().equals("presDeleteObj"))
		{
			prescription.removeObject(prescription.getSelectedIndex());			canvas.repaint();			return;
		}
	}
}